package com.yidu.BusinessParameters.mapper;

import com.yidu.BusinessData.entity.SecurityEntity;
import com.yidu.BusinessParameters.entity.Trade;
import com.yidu.CashAdministration.entity.MoneyAllocateEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 交易数据的接口
 *
 * @author zhouhongyu
 * @version 1.0
 * @since 2022/8/27
 */
@Mapper
public interface TradeMapper {
    /**
     * 查询条数
     *
     * @return
     */
    @Select("select count(*) from trade")
    public int selectCount();

    /**
     * 查询全部交易数据
     *
     * @param map
     * @return
     */
    @Select("SELECT * FROM trade a \n" +
            "JOIN SECURITY s ON a.securityId = s.securityId\n" +
            "JOIN cash b ON a.cashId = b.cashId \n" +
            "JOIN broker c ON a.brokerId = c.brokerId\n" +
            "JOIN TradingSeats d ON a.seatId = d.seatId where 1=1 ${strWreen} limit #{page},#{limit}")
    public List<Trade> selectTrade(Map map);

    /**
     * 增加交易数据
     *
     * @param trade
     * @return
     */
    @Insert("INSERT INTO trade (tradeId,fundId,securityId,cashId,brokerId,seatId,tradePrice,tradeDate,tradeNum,settleDate,tradeMoney,settleMoney,fees,transferFee,stampDuty,collectionFee,handlingFee,boodInterest,tradeType,settleState) " +
            "VALUE(#{tradeId},#{fundId},#{securityId},#{cashId},#{brokerId},#{seatId},#{tradePrice},#{tradeDate},#{tradeNum}," +
            "#{settleDate},#{tradeMoney},#{settleMoney},#{fees},#{transferFee},#{stampDuty},#{collectionFee},#{handlingFee},#{boodInterest},#{tradeType},#{settleState})")

    public int insertTrade(Trade trade);

    /**
     * 修改交易数据
     *
     * @param trade
     * @return
     */
    @Update("update trade set fundId=#{fundId},cashId=#{cashId},brokerId=#{brokerId},seatId=#{seatId},tradePrice=#{tradePrice},tradeType=#{tradeType},tradeNum=#{tradeNum},tradeDate=#{tradeDate}," +
            "settleDate=#{settleDate},tradeMoney=#{tradeMoney},settleMoney=#{settleMoney},boodInterest=#{boodInterest},stampDuty=#{stampDuty}," +
            "transferFee=#{transferFee},collectionFee=#{collectionFee},handlingFee=#{handlingFee},fees=#{fees},settleState=#{settleState} where securityId=#{securityId}")
    public int updateTrade(Trade trade);

    /**
     * 删除交易数据
     *
     * @param tradeId
     * @return
     */
    @Delete("delete from trade where tradeId = #{tradeId}")
    public int deleteTrade(String tradeId);




    //交易结算sql板块
    /**
     * 查询交易的已结算
     * @param map
     * @return
     */
    @Select("select * from trade a \n" +
            "JOIN security b ON a.securityId = b.securityId \n" +
            "JOIN cash c ON a.cashId = c.cashId \n" +
            "JOIN broker d ON a.brokerId = d.brokerId \n" +
            "JOIN tradingSeats e ON a.seatId = e.seatId where 1=1 and a.settleState=1 ${tradeType1} limit #{page},#{limit}")
    public List<Trade> selectSettlement(Map map);

    /**
     * 查询交易结算的未结算
     * @param map
     * @return
     */
    @Select("select * from trade a \n" +
            "JOIN security b ON a.securityId = b.securityId \n" +
            "JOIN cash c ON a.cashId = c.cashId \n" +
            "JOIN broker d ON a.brokerId = d.brokerId \n" +
            "JOIN tradingSeats e ON a.seatId = e.seatId where 1=1 and a.settleState=2 ${tradeType1} limit #{page},#{limit}")
    public List<Trade> selectSettlement2(Map map);

    /**
     * 修改未结算
     * @param tradeId
     * @return
     */
    @Update("update trade set settleState=1 where tradeId = #{tradeId}")
    public int updateSettlement(String tradeId);

    /**
     * 修改已结算
     * @param tradeId
     * @return
     */
    @Update("update trade set settleState=2 where tradeId = #{tradeId}")
    public int updateSettled(String tradeId);

    /**
     * 删除资金调拨的数据 --反结算
     */
   @Delete("delete from moneyAllocate where businessId = #{businessId}")
    public int deleteMoneyAllocate(String businessId);

}
